using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using RADB;


namespace Host
{
    public partial class Form1 : XtraForm
    {
        IDataTransfer DTO;
        public Form1()
        {
            InitializeComponent();
            InitGrid();
        }
        void InitGrid()
        {
            //BindingList<Person> gridDataList = new BindingList<Person>();
            //gridDataList.Add(new Person("Joth", "Smith"));
            //gridDataList.Add(new Person("Gabriel", "Smith"));
            //gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
            //gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
            //gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
            //gridControl.DataSource = gridDataList;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IDataTransferFactory.RegisterChannel();
            DTO = IDataTransferFactory.getDataTransferObject(Properties.Settings.Default.RemoteObjAdddress);
            DataTable tables = DTO.getTables();
            foreach (DataRow row in tables.Rows)
            {
                dataSetRA._Tables.Rows.Add(row.ItemArray);
            }
            tablesManagement1.SetSource(dataSetRA, null);
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            List<int> tablelist = new List<int>();
            List<DataSetRA.TablesRow> removeList = new List<DataSetRA.TablesRow>();
            DataTable tables;
            try
            {
                tables = DTO.getTables();
            }
            catch (Exception)
            {
                timer1.Enabled = false;
                MessageBox.Show("Cannot connect to server", "Server error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                timer1.Enabled = true;
                return;
            }
            foreach (DataRow row in tables.Rows)
            {
               DataSetRA.TablesRow tRow = dataSetRA._Tables.FindByID((int)row["ID"]);
               if (tRow == null)
               {
                   dataSetRA._Tables.Rows.Add(row.ItemArray);
               }
               else
               {
                   if (tRow.Status != (int)row["status"])
                   {
                       tRow.Status = (int)row["status"];
                   }
                   if (tRow.PosX != (double)row["PosX"])
                   {
                       tRow.PosX = (double)row["PosX"];
                   }
                   if (tRow.PosY != (double)row["PosY"])
                   {
                       tRow.PosY = (double)row["PosY"];
                   }
               }
               tablelist.Add(tRow.ID);
            }
            foreach (DataSetRA.TablesRow row in dataSetRA._Tables)
            {
                if (tablelist.IndexOf(row.ID)<0)
                {
                    removeList.Add(row);
                }
            }
            foreach (DataSetRA.TablesRow row in removeList)
            {
                dataSetRA._Tables.RemoveTablesRow(row);
            }
        }

        private void tablesManagement1_tableSetStatus(int tableid, _TablesManagement.Table.TableStatus status)
        {
            switch (status)
            {
                case _TablesManagement.Table.TableStatus.Available:
                    DTO.setTableAvailable(tableid);
                    break;
                case _TablesManagement.Table.TableStatus.Reserved:
                    
                    break;
                case _TablesManagement.Table.TableStatus.Waiting:
                    DTO.setTableWaiting(tableid);
                    break;
                case _TablesManagement.Table.TableStatus.Occupied:
                    DTO.setTableOccupied(tableid);
                    break;
                case _TablesManagement.Table.TableStatus.Dirty:
                    DTO.setTableDirty(tableid);
                    break;
                case _TablesManagement.Table.TableStatus.Cleaning:
                    DTO.setTableCleaning(1, tableid);
                    break;
                default:
                    break;
            }
        }

    }
}